﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.IO;
using System.Text.RegularExpressions;
using Jumbot.DataOper;

namespace Jumbot.DataOper.UI
{
    /// <summary>
    /// WebPage的通用基类。实现了一些常用操作。
    /// </summary>
    public abstract class PageUI : System.Web.UI.Page
    {
        /// <summary>
        /// 覆盖系统默认的错误页
        /// </summary>
        protected override void OnError(EventArgs e)
        {
            HttpContext ctx = HttpContext.Current;
            Exception exception = ctx.Server.GetLastError();
            string errorInfo =
                "\r\n<pre>Offending URL: " + ctx.Request.Url.ToString() +
                "\r\nSource: " + exception.Source +
                "\r\nMessage: " + exception.Message +
                "\r\nStack trace: " + exception.StackTrace + "</pre>";

            ctx.Response.Write(errorInfo);
            ctx.Server.ClearError();
            base.OnError(e);
        }
        /// <summary>
        /// 表示数据库访问对象。通常需要另外一层继承来实现站点相关的通用操作后再在页面中使用。
        /// </summary>
        public Jumbot.DataOper.DbOperHandler doh;
        /// <summary>
        /// 待实现的连接数据库函数。
        /// </summary>
        public abstract void ConnectDb();
        /// <summary>
        /// 连接Sql Server数据库。
        /// </summary>
        /// <param name="serverName">服务器地址。</param>
        /// <param name="userName">用户名。</param>
        /// <param name="password">密码。</param>
        /// <param name="dataBaseName">数据库名称。</param>
        public void ConnectDb(string serverName, string userName, string password, string dataBaseName)
        {
            System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection("server='" + serverName + "';uid=" + userName + ";pwd=" + password + ";database=" + dataBaseName);
            doh = new Jumbot.DataOper.SqlDbOperHandler(sqlConn);
        }
        /// <summary>
        /// 连接到一个Access数据库。
        /// </summary>
        /// <param name="dataBase">数据库名称。</param>
        public void ConnectDb(string dataBase)
        {
            System.Data.OleDb.OleDbConnection oleConn = new System.Data.OleDb.OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + this.Server.MapPath(dataBase));
            doh = new Jumbot.DataOper.OleDbOperHandler(oleConn);
        }

        /// <summary>
        /// 页面初始化的通用操作
        /// </summary>
        /// <param name="e"></param>
        override protected void OnInit(EventArgs e)
        {
            JbInit();
            base.OnInit(e);
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        protected virtual void JbInit()
        {
            this.Unload += new EventHandler(Jbpage_Unload);
        }
        /// <summary>
        /// 在客户端显示弹出对话框。
        /// </summary>
        /// <param name="msg">要显示的信息。</param>
        public void Alert(string msg)
        {
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">alert('" + msg + "')</script>");
        }
        /// <summary>
        /// 在客户端显示弹出对话框。
        /// </summary>
        /// <param name="name">脚本块标识。当同一页面要调用两个弹出框时需不同的标识，否则后者会覆盖前者。</param>
        /// <param name="msg">要显示的信息。</param>
        public void Alert(string name, string msg)
        {
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), name, "<script language=\"javascript\">alert('" + msg + "');</script>");
        }

        /// <summary>
        /// 在客户端执行一段脚本。
        /// </summary>
        /// <param name="name">脚本框表示。</param>
        /// <param name="cmd">要执行的命令。</param>
        public void JsExe(string name, string cmd)
        {
            //this.ClientScript.RegisterStartupScript(this.GetType(), name, "<script language=\"javascript\">" + cmd + ";</script>");
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), name, "<script language=\"javascript\">" + cmd + ";</script>");
        }

        /// <summary>
        /// 判断验证码是否符合要求
        /// </summary>
        /// <param name="code">用户输入的验证码</param>
        /// <returns>返回验证码是否正确</returns>
        public bool CheckValidateCode(string code)
        {
            try
            {
                if (code.ToUpper() != Session["jcms_validate_code"].ToString().ToUpper()) return false;
                return true;
            }
            catch (Exception e)
            {
                e.ToString();
                return false;
            }
        }
        /// <summary>
        /// 获得两个日期的间隔
        /// </summary>
        /// <param name="DateTime1">日期一。</param>
        /// <param name="DateTime2">日期二。</param>
        /// <returns>日期间隔TimeSpan。</returns>
        public TimeSpan DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            return ts;
        }

        /// <summary>
        /// 获得用户IP
        /// </summary>
        public string GetUserIp()
        {
            string ip;
            string[] temp;
            bool isErr = false;
            if (HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"] == null)
                ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            else
                ip = HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"].ToString();
            if (ip.Length > 15)
                isErr = true;
            else
            {
                temp = ip.Split('.');
                if (temp.Length == 4)
                {
                    for (int i = 0; i < temp.Length; i++)
                    {
                        if (temp[i].Length > 3) isErr = true;
                    }
                }
                else
                    isErr = true;
            }

            if (isErr)
                return "1.1.1.1";
            else
                return ip;
        }

        /// <summary>
        /// 获得当前程序名
        /// </summary>
        public string GetScriptName()
        {
            return HttpContext.Current.Request.ServerVariables["Script_Name"];
        }

        /// <summary>
        /// 格式化IP
        /// </summary>
        public string FormatIp(string ipStr)
        {
            string[] temp = ipStr.Split('.');
            string format = "";
            for (int i = 0; i < temp.Length; i++)
            {
                if (temp[i].Length < 3) temp[i] = Convert.ToString("000" + temp[i]).Substring(Convert.ToString("000" + temp[i]).Length - 3, 3);
                format += temp[i].ToString();
            }
            return format;
        }
        /// <summary>
        /// 来源地址
        /// </summary>
        public string GetRefererUrl()
        {
            if (HttpContext.Current.Request.ServerVariables["Http_Referer"] == null)
                return "";
            else
                return HttpContext.Current.Request.ServerVariables["Http_Referer"].ToString();
        }
        /// <summary>
        /// 当前地址
        /// </summary>
        public string GetCurrentUrl()
        {
            string strUrl;
            strUrl = HttpContext.Current.Request.ServerVariables["Url"];
            if (HttpContext.Current.Request.QueryString.Count == 0) //如果无参数
                return strUrl;
            else
            {
                return strUrl + "?" + HttpContext.Current.Request.ServerVariables["Query_String"];
            }

        }
        /// <summary>
        /// 当页面从内存卸载时发生，关闭数据库连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Jbpage_Unload(object sender, EventArgs e)
        {
            if (doh != null)
            {
                doh.Dispose();
            }
        }
    }
}
